package com.phpxiu.app.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.phpxiu.app.emotion.EmotionIcon;
import com.phpxiu.app.kkylive.KKYApp;
import com.phpxiu.app.kkylive.R;
import com.phpxiu.app.utils.KKYUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 吴金洲 on 2016/7/29
 */
public class EmotionGridAdapter extends BaseAdapter {
    public final static String TAG="EmotionGridAdapter";
    private List<EmotionIcon> pager=new ArrayList<EmotionIcon>();
    private LayoutInflater mInflater;
    private AbsListView.LayoutParams mParams=new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    private int mScreenWidth=(int)KKYApp.getInstance().screenWidth;
    private int size;
    private int height;
    public EmotionGridAdapter(Context context, List<EmotionIcon> pager) {
        this.pager = pager;
        mInflater=LayoutInflater.from(context);
        int realWidth=mScreenWidth- KKYUtil.dip2px(context, 30);
        size=realWidth/8;
        height= KKYUtil.dip2px(context,178);
        if(height/3<size){
            size=height/3;
        }
        mParams.height=size;
        mParams.width=size;
    }

    @Override
    public int getCount() {
        return pager.size();
    }

    @Override
    public Object getItem(int position) {
        return pager.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        EmotionIcon emotion=pager.get(position);
        if(emotion!=null){
            if(emotion.isDEl()){
                if(convertView==null||convertView.getId()==R.id.c2c_chat_emotion_grid_item){
                    convertView=mInflater.inflate(R.layout.c2c_chat_emotion_grid_item_del,null);
                    convertView.setLayoutParams(mParams);
                }
                convertView.setTag(emotion);
                return convertView;
            }else {
                if(convertView==null||convertView.getId()==R.id.c2c_chat_emotion_grid_item_del){
                    convertView=mInflater.inflate(R.layout.c2c_chat_emotion_grid_item_view,null);
                    convertView.setLayoutParams(mParams);
                }
                if(emotion.isEmpty()||emotion.getEmotion()==null||"".equals(emotion.getEmotion())) {
                    ((TextView) convertView).setText("");//空表情项
                    convertView.setTag(emotion);
                    return convertView;
                }
            }
            ((TextView) convertView).setText(emotion.getEmotion());//显示表情图片项
            convertView.setTag(emotion);
        }
        return convertView;
    }
}

